<?php $aLocales = osc_get_locales() ; ?>
<!DOCTYPE html>
<html dir="ltr" lang="<?php echo str_replace('_', '-', osc_current_user_locale()) ; ?>">
    <head>
        <?php osc_current_web_theme_path('head.php') ; ?>
        <meta name="robots" content="noindex, nofollow" />
        <meta name="googlebot" content="noindex, nofollow" />
        <script type="text/javascript">
            twitter_theme.text_select_subcategory = "<?php _e('Select a subcategory...', 'twitter_bootstrap') ; ?>" ;
            twitter_theme.category_selected_id    = "<?php echo item_selected_category_id() ; ?>" ;
            twitter_theme.subcategory_selected_id = "<?php echo item_selected_subcategory_id() ; ?>" ;
            twitter_theme.max_number_photos       = <?php echo osc_max_images_per_item() ; ?> ;
            twitter_theme.photo_remove_text       = "<?php _e('Remove', 'twitter_bootstrap') ; ?>" ;
            twitter_theme.max_images_fields_txt   = "<?php _e('Sorry, you have reached the maximum number of images per ad',  'twitter_bootstrap') ; ?>" ;
            twitter_theme.country_select_id       = "<?php echo get_country_id((osc_item() != null) ? osc_item() : array()) ; ?>" ;
            twitter_theme.region_select_id        = "<?php echo get_region_id((osc_item() != null) ? osc_item() : array()) ; ?>" ;
            twitter_theme.city_select_id          = "<?php echo get_city_id((osc_item() != null) ? osc_item() : array()) ; ?>" ;
            twitter_theme.ajax_url                = "<?php echo osc_base_url(true) . '?page=ajax' ; ?>" ;
            twitter_theme.text_select_country     = "<?php _e('Select a country...', 'twitter_bootstrap') ; ?>" ;
            twitter_theme.text_select_region      = "<?php _e('Select a region...', 'twitter_bootstrap') ; ?>" ;
            twitter_theme.text_select_city        = "<?php _e('Select a city...', 'twitter_bootstrap') ; ?>" ;
            twitter_theme.text_no_regions         = "<?php _e('No regions available', 'twitter_bootstrap') ; ?>" ;
            twitter_theme.text_no_cities          = "<?php _e('No cities available', 'twitter_bootstrap') ; ?>" ;
            twitter_theme.page                    = "edit" ;
            twitter_theme.item_id                 = "<?php echo osc_item_id() ; ?>" ;
            twitter_theme.delete_photo_txt        = "<?php _e('This action cannot be undone. Are you sure you want to continue?', 'twitter_bootstrap') ; ?>";
            
        </script>
        <script type="text/javascript" src="<?php echo osc_current_web_theme_js_url('bootstrap-tabs.js') ; ?>"></script>
        <script type="text/javascript" src="<?php echo osc_current_web_theme_js_url('item_form.js') ; ?>"></script>
        <?php item_category_select_js() ; ?>
        
    <script type="text/javascript">	
	
    function LoadAttributes() {			
            var cat_id=$("#catId").val();
			
	   if( cat_id != "" ) { 
            $.ajax({
                type: "POST",
                url: twitter_theme.ajax_url,
                data: { action: "runhook", hook: "item_" + twitter_theme.page, catId: cat_id, itemId: twitter_theme.item_id } ,
                dataType: 'html',
                success: function(data){
                    $("#plugin-hook").html(data);
					FinishLoad();
                }
            });  
        }		
    }
	
    function FinishLoad() {
		
            if($('#price').val()=="0") $('#price').val('');	            	
            numericInput('price');            
            $("#model").attr('disabled',false);		
            if($('#descId').val() == ' ') $('#descId').val('');	

            var type_id = VeicleTypeWrapper($("#catId").val());
	    $("#car_type").val(type_id); 
	    preloadAttr(type_id);    
                
            HideLoading();
	}
	
    function fillVeicleMake(cat){
		
            var type_id = VeicleTypeWrapper(cat);			
	    $("#car_type").val(type_id); 
	    preloadAttr(type_id);
            
            var url = '<?php echo osc_ajax_plugin_url("cars_attributes/ajax.php") . "&typeId="; ?>' + type_id;
            var result = '';
            if(type_id != '') {
                $("#make").attr('disabled',false);
                $.ajax({
                    type: "POST",
                    url: url,
                    dataType: 'json',
                    success: function(data){
                        var length = data.length;
                        if(length > 0) {
                            result += '<option value=""><?php _e("Select a make", 'cars_attributes'); ?></option>';
                            for(key in data) {
                                result += '<option value="' + data[key].pk_i_id + '">' + data[key].s_name + '</option>';
                            }
                            result += '<option value="-1"><?php _e("Others", 'cars_attributes'); ?></option>';
          		    $("#model").attr('disabled',false);
                        } else {
                            result += '<option value=""><?php _e('No results', 'cars_attributes'); ?></option>';
			    $("#make").attr('disabled',true);
                            $("#makePnl").css("display","none");
                            $("#titleAuxPnl").css("display","");
                        }
                        $("#make").html(result);
						
                          result += '<option value="" selected><?php _e('No results', 'cars_attributes'); ?></option>';
                          $("#model").attr('disabled',true);
                          $("#model").html(result);
                    }
                 });				
             } 
    }
		
    //Load
    $(document).ready(function() { 	

            LoadAttributes();	
            //Loading();

            $("select.category").change(function () {

                    var cat_id=$(this).val();				
                    if( cat_id != "" ) {
                            fillVeicleMake(cat_id);
                    }
            });		
    });

    function validateItem() {

        var isValid = validateItemAttr();
       
//        if ($("#make").val() == "") {
//            $("#makeInput").attr("class", "input-validation-error");
//            $("#makeVal").attr("class", "field-validation-error");
//            $("#make").focus();
//            isValid = false;
//        }
//        else {
//            $("#makeInput").attr("class", "input");
//            $("#makeVal").attr("class", "field-validation-valid");
//        }                
//        if ($("#model").val() == "") {
//            $("#modelInput").attr("class", "input-validation-error");
//            $("#modelVal").attr("class", "field-validation-error");
//            $("#model").focus();
//            isValid = false;
//        }
//        else {
//            $("#modelInput").attr("class", "input");
//            $("#modelVal").attr("class", "field-validation-valid");
//        }
//        if ($("#registration").val() == "") {
//            $("#registrationInput").attr("class", "input-validation-error");
//            $("#registrationVal").attr("class", "field-validation-error");
//            $("#registration").focus();
//            isValid = false;
//        }
//        else {
//            $("#registrationInput").attr("class", "input");
//            $("#registrationVal").attr("class", "field-validation-valid");
//        }

        if(isValid) {
            //Set title and descp value
            //var titleStr = $('#make option:selected').text() + ' - ' + $('#model option:selected').text();
            var titleStr = ItemTitle();
            var brandStr = ItemBrand();
            $('#titleId').val(titleStr);
            $('#meta_item_brand').val(brandStr);	
            if($('#descId').val() == '') $('#descId').val(' ');			

            Loading();
            return true;
        }
        return false;
    }
    </script>
	
    </head>
    <body>
        
          <?php while ( osc_has_item_meta() ) { 	
         if(osc_item_meta_name() == "Phone" ) { 
              $m_phone = osc_field(osc_item_meta(), 's_value', '');	             
            } 
         if(osc_item_meta_name() == "Brand" ) { 
              $m_brand = osc_field(osc_item_meta(), 's_value', '');						
            } 
        } ?>
        
	<div id="curtain" style="display: inline;"></div>
			<div id="clock" style="display: inline; "></div>	
        <?php osc_current_web_theme_path('header.php') ; ?>
        <div class="container margin-top-10">
            <?php twitter_show_flash_message() ; ?>
        </div>
        <div class="container item-edit">
            <?php echo twitter_breadcrumb('&raquo;') ; ?>
            <div class="row">
                <div class="span16 columns">
                    <form class="well" name="item" action="<?php echo osc_base_url(true) ; ?>" method="post" enctype="multipart/form-data">
                        <input type="hidden" name="action" value="item_edit_post" />
                        <input type="hidden" name="page" value="item" />
                        <input type="hidden" name="id" value="<?php echo osc_item_id() ; ?>" />
                        <input type="hidden" name="secret" value="<?php echo osc_item_secret() ; ?>" />
                        <fieldset>
							<br/>
                            <span style="float:left;font-size:20px;font-weight:bold;"><?php _e('Update your item', 'twitter_bootstrap') ; ?></span>
							<span  class="label grey" style="text-transform: lowercase;float:right;font-size:18px;font-weight:bold;"><?php _e("Vehicle reference", 'twitter_bootstrap'); ?>: <?php echo osc_item_id(); ?></span>
							<br/>
							<hr/>
							<br/>
                            <!-- category input -->
                            <div class="clearfix">
                                <label><?php _e('Category', 'twitter_bootstrap') ; ?></label>
                                <div class="input">
                                    <?php item_category_select_edit() ; ?>
                                </div>
                            </div>
                            <!-- category input end -->
                            <!-- Truck info -->			
                            
                             <div class="clearfix">
                                <div id="plugin-hook"></div>
                            </div>
                            <!-- truck info end -->
                            <hr/>
                            <!-- title and description -->
                            <h3><?php _e('Vehicle Details', 'twitter_bootstrap') ?></h3>
                              <?php if( osc_price_enabled_at_items() ) { ?>
                                <!-- price -->
                                <div class="clearfix">
                                    <label for="price"><?php _e('Price', 'twitter_bootstrap') ; ?></label>
                                    <div class="input">
                                        <?php item_price_input( ) ; ?>
                                        <?php item_currency_select( ) ; ?>
                                        <span class="help-block">
                                            <?php _e("<strong>Note:</strong> Leave it blank if you don't want to publish a price.", 'twitter_bootstrap') ; ?>
                                        </span>
                                    </div>
                                </div>
                                <!-- price end -->
                            <?php } ?>
                            <div class="clearfix">
                                <?php //if( count($aLocales) > 1 ) { ?>
                                    <?php //item_title_description_multilanguage_box(__('Title', 'twitter_bootstrap'), __('Description', 'twitter_bootstrap'), $aLocales) ; ?>
                                <?php //} else { ?>
                                    <?php item_title_description_box(__('Title', 'twitter_bootstrap'), __('Description', 'twitter_bootstrap'), $aLocales) ; ?>
                                <?php //} ?>
                            </div>
                            <!-- title and description end -->                          
                            <hr/>
                            <!-- location -->
                            <h3><?php _e('Location', 'twitter_bootstrap') ?></h3>
                            <?php item_country_box(__("Country", "twitter_bootstrap"), __("Select a country...", "twitter_bootstrap")) ; ?>
                            <?php item_region_box(__("Region", "twitter_bootstrap"), __("Select a region...", "twitter_bootstrap")) ; ?>
                            <?php //item_city_box(__("City", "twitter_bootstrap"), __("Select a city...", "twitter_bootstrap")) ; ?>
                            <div class="clearfix">
                                <label><?php echo __("City", "twitter_bootstrap"); ?></label>
                                <div class="input">
                                <select id="city_id" name="cityId" class="city_id"></select>
                                </div>
                            </div>
                                
                            
                            <!-- location end -->
							<hr/>
							<!-- seller -->
							<h3><?php _e("Seller's information", "twitter_bootstrap"); ?></h3>
							<br/>
										<div class="clearfix">
											<label><?php _e('Name', 'twitter_bootstrap') ; ?></label>
											<div class="input">
												<?php echo item_contact_name_input_edit() ; ?>
											</div>
										</div>
										<div class="clearfix">
											<label><?php _e('E-mail', 'twitter_bootstrap') ; ?></label>
											<div class="input">
												<?php echo item_contact_mail_input_edit() ; ?>
											</div>
										</div>
										
										<div class="clearfix">											
											<?php //if( osc_count_item_meta() > 0 ) { ?>
											<div class="meta_list">
												<?php //while ( osc_has_item_meta() ) { ?>	
												
												<label for="meta_item_phone"><?php _e('Phone', 'twitter_bootstrap') ; ?></label>
												<div class="input">
												<input id="meta_item_phone" type="text" value="<?php echo $m_phone; ?>" name="meta[1]" />
												</div>													
												<?php  //} ?>
											</div>
											<?php //} ?>	
										</div>
										<div style="display:none;">
                                                                                    <input id="meta_item_brand" type="text" name="meta[2]" value="<?php echo $m_brand; ?>" />
                                                                                </div>
                                                                                
										<div class="clearfix">
											<div class="input">
												<ul class="inputs-list">
													<li>
														<label for="showEmail">
															<?php echo item_contact_show_email_checkbox() ; ?>
															<span><?php _e('Show phone contact on the item page', 'twitter_bootstrap'); ?></span>
														</label>
													</li>
												</ul>
											</div>
										</div>																				
										<!-- seller end -->
										<hr/>
										
                            <?php if( osc_images_enabled_at_items() ) { ?>
                                <!-- photo -->
                                <h3><?php _e('Photos', 'twitter_bootstrap') ; ?></h3>
                                <div class="clearfix photos">
                                    <?php 
                                        $resources = osc_get_item_resources();
                                        if( $resources != null && is_array($resources) && count($resources) > 0) { ?>
                                            <ul class="media-grid" >
                                            <?php foreach($resources as $r) { ?>
                                            <li id="<?php echo $r['pk_i_id'] ; ?>" fkid="<?php echo $r['fk_i_item_id'] ; ?>" name="<?php echo $r['s_name'] ; ?>">
                                                <a href="javascript://">
                                                    <img class="thumbnail quimby_search_image" src="<?php echo osc_base_url() . $r['s_path'] . $r['pk_i_id'] . '_thumbnail.' . $r['s_extension'] ; ?>" />
                                                </a>
                                                
                                                <span style="float:right;">
                                                    <a class="btn danger" href="javascript:delete_image(<?php echo $r['pk_i_id'] . ", " . $r['fk_i_item_id'] . ", '" . $r['s_name'] . "', '" . Params::getParam('secret') . "'" ; ?>);" class="delete"><?php _e('Delete', 'twitter_bootstrap') ; ?></a>
                                                </span>
                                            </li>
                                            <?php } ?>
                                            </ul>
                                        <?php } ?>
                                    <?php if(osc_max_images_per_item()==0 || (osc_max_images_per_item()!=0 && osc_count_item_resources()<  osc_max_images_per_item())) { ?>
                                    <div class="input input-file">
                                        <input type="file" name="photos[]" size="35" />
                                    </div>
                                    <?php } ?>
                                    <div class="more-photos">
                                    </div>
                                    <div class="input">
                                        <a href="javascript://" onclick="return add_photo_field();">[ <?php _e('Add new photo', 'twitter_bootstrap'); ?> ]</a>
                                    </div>
                                </div>
                                <!-- photo end -->
                            <?php } ?>
							
							
							
                            <?php if( osc_recaptcha_items_enabled() ) { ?>
                            <div class="clearfix">
                                <?php osc_show_recaptcha(); ?>
                            </div>
                            <?php } ?>
                            <div class="actions">
                                <button class="btn success" type="submit" onclick="return validateItem();"><?php _e('Update', 'twitter_bootstrap') ; ?></button>
                                <a class="btn" href="javascript:history.back(-1)"><?php _e('Cancel', 'twitter_bootstrap') ; ?></a>
                            </div>
                        </fieldset>
                    </form>
                </div>
            </div>
        </div>
        <script type="text/javascript">
            var text_error_required = '<?php _e('This field is required', 'twitter_bootstrap') ; ?>' ;
        </script>
        <script type="text/javascript" src="<?php echo osc_current_web_theme_js_url('item_post.js') ; ?>"></script>
        <?php osc_current_web_theme_path('footer.php') ; ?>
    </body>
</html>